module V{
    /** 李逵捕鱼8人 游戏界面 */
    export class Buyu extends GameView{
        /** 背景图片 */
        private bg:eui.Image;
        /** 显示容器 */
        private showCon:eui.Group;
        /** 子弹ID */
        private bId:number;
        /** 子弹列表 */
        private bList:Array<BuyuBullet>;
        /** 鱼列表 */
        private fList:Array<BuyuFish>;
        /** 子弹值 */
        private mul:number;
        /** 子弹类型 */
        private kind:number;
        /** 隐藏显示按钮 */
        private btMenu:eui.Image;
        /** 退出按钮 */
        private btExit:eui.Image;
        
        public constructor(){
            super();
            this.Res = ["buyu"];
            this.MAX_PLAYER = 8;
            this.skinName = "BuyuSkin";
            this.bList = [];
            this.fList = [];
            this.bId = 1;
            this.kind = 3;
        }

        /** 子元素创建完成后 */
        protected childrenCreated():void{
            super.childrenCreated();
            this.addEventListener(egret.Event.ENTER_FRAME, this.onEventFrame, this);
            this.showCon.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
            //this.scaleX = this.stage.width / this.width;
            //this.scaleY = this.stage.height / this.height;
            //console.log("childrenCreated", this.width, this.stage.width);
        }

        /** 收到网络消息 */
        public onNetData(s:number, d:any):void{
            let jd = null;
            switch(s){
                case 101:{ //鱼的轨迹
                    let ct = (d.length - 8) / 57;
                    jd = [];
                    for(let i=0;i<ct;i++){
                        jd.push(D.App.b2j(d, [["A","pos",10,"F"],["I","init_count"],["B","cmd_version"],["I","fish_kind"],["I","fish_id"],["I","trace_type"]]));
                    }
                    for(let i=0;i<ct;i++){
                        let f:BuyuFish = null;
                        for(let fi=0;fi<this.fList.length;fi++){
                            if(this.fList[fi].isClose()){
                                f = this.fList[fi];
                                break;
                            }
                        }
                        if(!f){
                            f = new BuyuFish(jd[i].fish_kind+1, jd[i].pos, jd[i].init_count, jd[i].fish_id);
                            this.fList.push(f);
                        }else{
                            f.updata(jd[i].fish_kind+1, jd[i].pos, jd[i].init_count, jd[i].fish_id);
                        }
                        f.mv && this.showCon.addChild(f.mv);
                    }
                    break;
                }case 102:{ //上下分
                    jd = D.App.b2j(d, [["S","chair_id"],["L","swap_fish_score"],["L","exchange_fish_score"]]);
                    D.App.table.players[jd.chair_id].score = jd.exchange_fish_score;
                    let p:BuyuPlayerView = this.playerList[jd.chair_id] as BuyuPlayerView;
                    p.upValue(0, 0);
                    break;
                }case 103:{ //开火
                    jd = D.App.b2j(d, [["I","bullet_kind"],["I","bullet_id"],["S","chair_id"],["S","android_chairid"],["F","angle"],["I","bullet_mulriple"],["I","lock_fishid"],["L","fish_score"],["F","fire_speed"]]);
                    if(jd.chair_id < this.MAX_PLAYER){
                        this.sendBullet(jd.chair_id, 180/(Math.PI/jd.angle), jd.bullet_mulriple, jd.bullet_kind, jd.bullet_id);
                        let p:BuyuPlayerView = this.playerList[jd.chair_id] as BuyuPlayerView;
                        p.upValue(jd.bullet_mulriple, -jd.bullet_mulriple);
                    }
                    break;
                }case 105:{ //捕到鱼了
                    jd = D.App.b2j(d, [["S","chair_id"],["I","fish_id"],["I","fish_kind"],["B","bullet_ion"],["L","fish_score"],["L","fish_caijin_score"],["I","app"]]);
                    if(jd.chair_id < this.MAX_PLAYER){
                        let p:BuyuPlayerView = this.playerList[jd.chair_id] as BuyuPlayerView;
                        p.upValue(0, jd.fish_score);
                        let gp:egret.Point = p.txtname.parent.localToGlobal(p.txtname.x, p.txtname.y)
                        //寻找鱼
                        for(let i=0;i<this.fList.length;i++){
                            if(this.fList[i].fishid == jd.fish_id){
                                this.fList[i].dead();
                                let mv = D.App.getMovieClip("fish3", "gold");
                                mv.x = this.fList[i].mv.x;
                                mv.y = this.fList[i].mv.y;
                                this.showCon.addChild(mv);
                                mv.gotoAndPlay("Move", -1);
                                egret.Tween.get(mv).to({x:gp.x, y:gp.y}, 3000).call(()=>{
                                    this.showCon.removeChild(mv);
                                });
                                break;
                            }
                        }
                    }
                    break;
                }case 110:{ //伤害了李逵
                    jd = D.App.b2j(d, [["S","chair_id"],["I","fish_id"],["I","fish_mulriple"]]);
                    break;
                }
            }
            console.log("buyu", s, d.length, JSON.stringify(jd));
        }
        /** 同步桌子 */
        public onSyncData(d:any):void{
            let jd = D.App.b2j(d, [["I","V"],["A","fishscore",8,"L"],["A","efishscore",8,"L"],["A","multiple",2,"I"],["B","isyuzhen"]]);
            console.log("onSyncData", jd);
            this.showPlayer(65535);
            this.mul = jd.multiple[0];
            for(let i=0;i<this.MAX_PLAYER;i++){
                let p:BuyuPlayerView = this.playerList[i] as BuyuPlayerView;
                p.upValue(this.mul);
                if(i == 0){
                    console.log("pl", p.y, this.stage.height);
                }
            }
            //换游戏币
            //let sd = D.App.createBin(9, 101, 1);
            //sd.writeByte(1);
            //D.App.net.send(sd);
            //let b = new BuyuBullet();
            //b.setAngle(90, 12, 0, 0);
            //this.showCon.addChild(b.mv);
        }
        /** 每帧处理 */
        public onEventFrame():void{
            for(let i=0;i<this.bList.length;i++){
                this.bList[i].move();
            }
            //检测碰撞
            let sd = new egret.ByteArray();
            sd.endian = egret.Endian.LITTLE_ENDIAN;
            for(let i=0;i<this.bList.length;i++){
                if(this.bList[i].isClose()) continue;
                let len = this.bList[i].mv.height / 2;
                for(let j=0;j<this.fList.length;j++){
                    if(!this.fList[j]) continue;
                    if(this.fList[j].isClose()) continue;
                    let x = this.bList[i].mv.x - this.fList[j].mv.x, y = this.bList[i].mv.y - this.fList[j].mv.y;
                    if(Math.sqrt(x*x + y*y) < len){
                        sd.writeUnsignedShort(this.bList[i].chairId);
                        sd.writeInt(this.fList[j].fishid);
                        sd.writeInt(this.bList[i].bulletKind);
                        sd.writeInt(this.bList[i].bulletId);
                        sd.writeInt(this.bList[i].bulletMul);

                        this.bList[i].del();
                        break;
                    }
                }
            }
            if(sd.length > 0){
                let ssd = D.App.createBin(9, 103, sd.length);
                ssd.writeBytes(sd);
                D.App.net.send(ssd);
            }
        }
        /** 触摸背景 */
        public onTap(e:egret.TouchEvent):void{
            let c = D.App.userData.chairID, p:BuyuPlayerView = this.playerList[c] as BuyuPlayerView;
            let gp:egret.Point = p.gun.parent.localToGlobal(p.gun.x, p.gun.y), jd = 180/(Math.PI/Math.atan2(e.stageY-gp.y,e.stageX-gp.x));
            console.log("bull", c, gp.x, gp.y, e.stageX, e.stageY, jd, this.bId);

            this.bId++;
            this.sendBullet(c, jd, this.mul, this.kind, this.bId);
            p.upValue(this.mul, -this.mul);

            let sd = D.App.createBin(9, 102, 29);
            sd.writeInt(this.bId);
            sd.writeInt(this.kind);
            sd.writeByte(1);
            sd.writeFloat(Math.PI/(jd*180));
            sd.writeInt(this.mul);
            sd.writeInt(-1);
            sd.writeInt(Date.now()/1000);
            sd.writeFloat(0.2);
            D.App.net.send(sd);
        }
        /** 发射子弹 */
        private sendBullet(c:number, jd:number, mul:number, kind:number, id:number){
            let p:BuyuPlayerView = this.playerList[c] as BuyuPlayerView;
            let gp:egret.Point = p.gun.parent.localToGlobal(p.gun.x, p.gun.y);
            if(c < 3){
                p.gun.rotation = jd + 90;
            }else if(c == 3){
                p.gun.rotation = jd + 180;
            }else if(c == 7){
                p.gun.rotation = jd;
            }else{
                p.gun.rotation = jd - 90;
            }
            //console.log("rotation", p.gun.rotation);
            if(p.gun.rotation > 90){
                p.gun.rotation = -(p.gun.rotation - 180);
            }else if(p.gun.rotation < -90){
                p.gun.rotation = -(p.gun.rotation + 180);
            }
            let b:BuyuBullet = null;
            for(let i=0;i<this.bList.length;i++){
                if(this.bList[i].isClose()){
                    b = this.bList[i];
                    break;
                }
            }
            if(b == null){
                b = new BuyuBullet();
                this.bList.push(b);
            }
            b.setAngle(jd + 90, 12, gp.x, gp.y);
            b.chairId = c;
            b.bulletMul = mul;
            b.bulletKind = kind;
            b.bulletId = id;
            this.showCon.addChild(b.mv);
            b.move(80);
        }
        /** 重置椅子 */
        public resetChair():boolean{
            if(D.App.userData.chairID < this.MAX_PLAYER && this.playerList[D.App.userData.chairID].chairId != D.App.userData.chairID) return true;
            return false;
        }
        /** 椅子号转位置 */
        public c2p(c):number{
            return c;
        }
    } 
}